package com.thinktank.controller;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.thinktank.dao.*;
import com.thinktank.vo.*;
import static com.thinktank.util.DbUtil.*;

public class GuestMainController implements Controller {
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 정보가 있으면 받고 없으면 초기화
		int page = 1;
		String pg = request.getParameter("pg");
		String field = request.getParameter("field");
		String query = request.getParameter("query");
		HttpSession session = request.getSession();
		String url = (String)session.getAttribute("url");
		
		if(pg != null)
			page = Integer.parseInt(pg);
		if(field == null)
			field = "GS_WRITER";
		if(query == null)
			query = "";
		else
			query = new String(query.getBytes("ISO-8859-1"), "UTF-8");
		
		// 연결 후 DB 작업
		Connection con=getConnection();
		GuestDao dao = new GuestDao(con);
		List<GuestVo> list = dao.getAllGuestVos(page, field, query, url);
		int count = dao.getCount(field, query);
		Member master = dao.getMaster(url);
		close(con);
		
		System.out.println(master);
		// 세팅 후 넘기기
		request.setAttribute("list", list);
		request.setAttribute("query", query);
		request.setAttribute("count", count);
		request.setAttribute("p", 5);
		session.setAttribute("master", master);
		
		return "guest_main.jsp";
	}
}
